package com.deyuanyun.pic.ctrl.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import com.deyuanyun.pic.ctrl.controller.dto.exinsp.PipeBaseQuery;
import com.deyuanyun.pic.ctrl.controller.vo.defect.CoatingStatisticsObjVO;
import com.deyuanyun.pic.ctrl.mapper.dao.StatisticsDao;
import com.deyuanyun.pic.ctrl.service.ExternalInspProjectService;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.ctrl.mapper.CpTestDashboardExpandMapper;
import com.deyuanyun.pic.ctrl.service.CpTestDashboardExpandService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationQueryVO;
import com.deyuanyun.pic.settings.domain.util.DomainCollectionsUtil;
import com.deyuanyun.pic.settings.mapper.pipe.PipeLocationMapper;

@Service
public class CpTestDashboardExpandServiceImpl implements CpTestDashboardExpandService {

    private @Resource PipeLocationMapper pipeLocationMapper;
    private @Resource CpTestDashboardExpandMapper cpTestDashboardExpandMapper;
    @Resource
    private ExternalInspProjectService externalInspProjectService;
    @Resource
    private StatisticsDao statisticsDao;
    
    @Override
    public Map<String, BigDecimal> queryCpTestStationProtectionRatio(String pipeLineId, Integer beginMileage, Integer endMileage) {
        
        Map<String, BigDecimal> map = cpTestDashboardExpandMapper.queryCpTestStationProtectionRatio(pipeLineId, beginMileage, endMileage);
        return map;
    }

    @Override
    public Map<String, BigDecimal> queryCpTestStationOperatingRatio(String pipeLineId, BigDecimal beginMileage, BigDecimal endMileage) {
        
        
        //先查询出来绝对距离中的场站阀室
        PipeLocationQueryVO pipeLocationQuery = new PipeLocationQueryVO();
        pipeLocationQuery.setPipeLineId(pipeLineId);
        pipeLocationQuery.setBeginMileage(beginMileage);
        pipeLocationQuery.setEndMileage(endMileage);
        pipeLocationQuery.setTargetType("valveRoomStation");
        List<PipeLocationDTO> valveRoomList = pipeLocationMapper.queryPipeLocation(pipeLocationQuery);
        if(valveRoomList == null || valveRoomList.isEmpty()){
            return null;
        }
        String[] pipeValveroomIds = DomainCollectionsUtil.toOneFieldValues(valveRoomList, "id");
        Map<String,BigDecimal> map = cpTestDashboardExpandMapper.queryCpTestStationOperatingRatio(pipeValveroomIds);
        
        return map;
    }

    @Override
    public List<Map<String,Object>> queryCpTestStationRecode(String pipeLineId,Integer beginMileage, Integer endMileage) {
        List<Map<String,Object>> map = cpTestDashboardExpandMapper.queryCpTestStationRecode(pipeLineId, beginMileage, endMileage);
        return map;
    }


    @Override
    public List<Object[]> queryCoatingGrade(PipeBaseQuery pipeBaseQuery) {
        pipeBaseQuery.setExinspTaskId(this.getNewestExinspTaskIds(pipeBaseQuery.getPipeLineId()));
        List<Object[]> tempArray = new ArrayList<Object[]>();
        List<CoatingStatisticsObjVO> list = statisticsDao.queryExinspOcgrade(pipeBaseQuery);
        for (CoatingStatisticsObjVO coatingStatisticsObjVO : list){
            Object[] array = new Object[3];
            array[0] = coatingStatisticsObjVO.getMileage();
            array[1] = coatingStatisticsObjVO.getEndMileage();
            array[2] = coatingStatisticsObjVO.getSvalue();
            tempArray.add(array);
        }
        return tempArray;
    }

    @Override
    public List<CoatingStatisticsObjVO> queryExinspPcmAcvg(PipeBaseQuery pipeBaseQuery) {
        pipeBaseQuery.setExinspTaskId(this.getNewestExinspTaskIds(pipeBaseQuery.getPipeLineId()));
        return statisticsDao.queryExinspPcmAcvg(pipeBaseQuery);
    }

    @Override
    public List<CoatingStatisticsObjVO> queryExinspCipsDcvg(PipeBaseQuery pipeBaseQuery) {
        pipeBaseQuery.setExinspTaskId(this.getNewestExinspTaskIds(pipeBaseQuery.getPipeLineId()));
        return statisticsDao.queryExinspCipsDcvg(pipeBaseQuery);
    }

    /**
     * 得到最新的任务编号
     *
     * @param pipeLineId
     * @return
     */
    private String getNewestExinspTaskIds(String pipeLineId) {
        List<PipeLineNewTaskVO> pipeLineNewTaskVOList = externalInspProjectService.queryNagmExternalTaskMerge(pipeLineId, new Date(), true);
        StringBuffer stringBuffer = new StringBuffer(256);
        for (PipeLineNewTaskVO pipeLineNewTaskVO : pipeLineNewTaskVOList){
            stringBuffer.append("'").append(pipeLineNewTaskVO.getId()).append("'").append(",");
        }
        String exinspTaskIds = null;
        if (stringBuffer.length() > 0) {
            exinspTaskIds = stringBuffer.deleteCharAt(stringBuffer.length()-1).toString();
        }
        return exinspTaskIds;
    }
}
